;(function($){
	$.frontspa_brxsxm = $.frontspa_brxsxm || {}
	$.fn.frontspa_brxsxm = function(opts){
		var el = this;
		var opt = {
			wait : function(){},
			goon : function(){},
			xsxm : [],
			cbbh:"",
			load_cbbh : {},
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
		$.frontspa_brxsxm = $.extend($.frontspa_brxsxm,app);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		this.initSubledgerGrid();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var html = 	'<div id="brxsxmGrid" class="widget-box">'+
							'<div class="widget-header">'+
								'<h5 class="widget-title">'+
									$.i18n.propJsp("销售细目")+
								'</h5>'+
							'</div>'+
							'<div class="widget-body">'+
								'<table id="subledgerGridList"></table>'+
							'</div>'+
						'</div>'+
						'<div id="brzdDcode" class="widget-box">'+
							'<div class="widget-header hide">'+
								'<h5 class="widget-title">'+
									$.i18n.propJsp("科目列表")+
								'</h5>'+
							'</div>'+
							'<div class="widget-body">'+
								'<div id="cbbhGrid" style="height:139px;overflow-x:auto;white-space:nowrap;margin:0px 10px 0px 10px;"></div>'+
							'</div>'+
						'</div>';
			el.html(html);
			t.setsize();
		},
		initSubledgerGrid : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			var grid_selector = el.find("#subledgerGridList");
			var h =  el.find("#brxsxmGrid").height() - 39 - 42 - 21
			grid_selector.jqGrid({
				datatype:"local",
				height:h,
				data :[],
				width:el.find("#brxsxmGrid").width(),
				colNames:['序号','NO','编号','名称',"单位","数量","金额","贷方金额","借贷","fk","部门号","原NO","日期","时间","台号"],
				colModel:
					[
					{name:'xh',index:'xh',width:100,key:true,},//'序号',
					{name:'no',index:'no',width:100,},//'NO',
					{name:'bh',index:'bh',width:100,},//'编号',
					{name:'mc',index:'mc',width:200,},//'名称',
					{name:'dw',index:'dw',width:50,align:"center"},//"单位",
					{name:'sl',index:'sl',width:100,align : "right",},//"数量",
					{name:'je',index:'je',width:100,align : "right",formatter : "number",thousandsSeparator : ",",decimalPlaces : 2,},//"金额",
					{name:'df_je',index:'df_je',width:100,align : "right",formatter : "number",thousandsSeparator : ",",decimalPlaces : 2,},//"贷方金额",
					{name:'jd',index:'jd',width:100,},//"借贷",
					{name:'fk',index:'fk',width:100,},//"fk",
					{name:'gzbmh',index:'gzbmh',width:100,},//"部门号",
					{name:'yno',index:'yno',width:100,},//"原NO",
					{name:'rq',index:'rq',width:80,},//"日期",
					{name:'sj',index:'sj',width:60,},//"时间",
					{name:'th',index:'th',width:80,},//"台号"
					],
				sortname : "th",
				sortorder : "asc",
				shrinkToFit:false,
				multiselect:true, 
				footerrow : true,
			    onSelectRow:function(rowid){
		        },
	        	ondblClickRow: function(rowid){ 
	        	},
			});
		},
		setsize : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var winh = $(window).height();
			var top = el.find("#brxsxmGrid").offset().top
			var h = winh - top - 147-94;
			el.find("#brxsxmGrid").css({"height":h});
		},
		getSubLedgerDetail : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			param.wait();
			var arg = {"arg":{"nos":param.nos,"cbbh":param.cbbh}};
			jQuery.ajax({  
	    		type : 'POST',
	    		url : $.HSAPI_AJAXURL+$.HSAPI_PROCEDURE2_LIST,
	    		data : $.HSAPI_ASKJSONINFO(JSON.stringify(arg),'SPAOrderSubLedger_Search_Detail','{}'),
	    		dataType : 'json',  
	    		success : function(data){
	    			param.goon();
	    			if(data.RETURN==1){
	    				param.xsxm = data.xsxm;
	    				
	    				t.freshCbbhGrid(data.cbbh);
	    				t.freshSubledgerGrid();
	    			}else{
						if(data.hasOwnProperty("gritter")){
							if(data.hasOwnProperty("gritterstyle"))
								jf_successDelayed(data.gritter,2000,data.gritterstyle);
							else
								jf_successDelayed(data.gritter,2000); 
						}else{
		    				jf_alert(data.MSG)
						}
					}
	    		},  
	    		error : function(data){
	    			param.goon();
	    			jf_alert(JSON.stringify(data));
	    		}  
	    	});
		},
		freshCbbhGrid:function(cbbh_arr){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var new_json = {}
			$.each(cbbh_arr,function(key,value){
				var bh = value.bh?value.bh:"-1"
				if(bh=="-1"){
					value.bh = "-1"
					value.mc = ""
				}
				new_json[bh] = value
			})
			console.log(JSON.stringify(new_json))
			
			var extendJson = {}
			for(var key in param.load_cbbh){
				extendJson[key] = param.load_cbbh[key]
				extendJson[key]['amount'] = "0.00"
			}
			param.load_cbbh = $.extend(extendJson,new_json);
			console.log(JSON.stringify(param.load_cbbh))
			
			
			$("#cbbhGrid").empty()
			var new_arr = []
			for(var key in param.load_cbbh){
				new_arr.push(param.load_cbbh[key])
			}
			$.each(new_arr,function(key,value){
				var checked=""
				if($.inArray(value.bh,param.cbbh.split(",")) >= 0){
					checked = "checked=true"
			    }
				var html = 	'<div style="">'+
								'<label class="control-label no-margin">'+
									'<input name="'+value.bh+'" value="yes" type="checkbox" class="ace cbbhBtn" '+checked+' onchange="this.value=(this.checked==true?1:0)" >'+
									'<span class="lbl" style="color:#428bca">'+value.mc+'</span>'+
									'<span class="line-height-1 bigger-110" style="color:#69aa46 !important">'+value.amount+'</span>'+
								'</label>'+
							'</div>';
				if( (key+1)%2 ==0 ){// 偶数    
					$("#cbbhGrid").find(".cbbhBox[data-num='"+(key)+"']").append(html)
				}else{// 奇数  
					var boxHtml = 	'<span class="cbbhBox" data-num='+(key+1)+'  style="display: inline-block;vertical-align: top;margin-right:30px;margin-top:15px;height: 90px;line-height:45px;">'+
									'</span>';
					$("#cbbhGrid").append(boxHtml)
					$("#cbbhGrid").find(".cbbhBox[data-num='"+(key+1)+"']").append(html)
					
				}
			})
			
			$(".cbbhBtn").unbind("click").click(function(){
				var cbbh = []
				$(".cbbhBtn").each(function(){
					if(this.checked){
						cbbh.push($(this).attr("name"))
					}
				})
				param.cbbh = cbbh.join(",")
				t.getSubLedgerDetail();
			})
		},
		freshSubledgerGrid : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			var grid_selector = el.find("#subledgerGridList");
			grid_selector.jqGrid("clearGridData");
			var ds = param.xsxm;
			
			if(ds.length!=0){
				for ( var i = 0; i < ds.length; i++){
					grid_selector.jqGrid('addRowData', ds[i].xh, ds[i],"last");
					if(ds[i].sele==1){
						grid_selector.jqGrid('setSelection',ds[i].xh);
					}
				}
			}
			var sl = grid_selector.getCol('sl',false,'sum');
			var je = grid_selector.getCol('je',false,'sum');
			var df_je = grid_selector.getCol('df_je',false,'sum');
	      	//将统计数据放到统计行上
			grid_selector.footerData("set",{sl:sl});
			grid_selector.footerData("set",{je:je});
			grid_selector.footerData("set",{df_je:df_je});  
			
			
			
		}
	}
})(jQuery);